"use strict"

/* 箭头函数 */
{
    let fn = x => x * x;
    // 相当于
    let fn = function (x) {
        return x * x;
    };
}

{
    let fn = x => {
        if (x > 0) {
            return x * x;
        }
        else {
            return -x * x;
        }
    };
}

{
    // 两个参数
    let fn = (x, y) => x * x + y * y;

    // 没有参数
    let pi = () => 3.14;

    // 可变参数
    let fn = (x, y, ...rest) => {
        let i, sum = x + y;
        for (let i = 0; i < rest.length; i++) {
            sum += rest[i];    
        }

        return sum;
    };
}

{
    // 虽然没有返回 SyntaxError , 但是我的 Edge 返回了 undefined
    let fn = x => { foo: x };

    let fn = x => ({foo: x});
    fn(1);   // > {foo: 1}
}

{
    let obj = {
        birth: 1990,
        getAge: function () {
            var b = this.birth;
            console.log(this.birth)
            var fn = function () {
                return new Date().getFullYear() - this.birth;
            };

            return fn();
        }
    };
}

{
    let obj = {
        birth: 1990,
        getAge: function () {
            var b = this.birth;
            console.log(this.birth) // 1990
            var fn = () => new Date().getFullYear() - this.birth;
            // 上面的 this 指向 obj 对象

            return fn();
        }
    };

    obj.getAge();   // 31
}

{
    let obj = {
        birth: 1990,
        getAge: function (year) {
            var b = this.birth;
            console.log(this.birth) // 1990
            var fn = (y) => y - this.birth;
            // 上面的 this 指向 obj 对象

            return fn.call({birth: 2000}, year);
        }
    };

    obj.getAge(2015);   // 31
}

var arr = [10, 20, 1, 2];
arr.sort((x, y) => x - y);
console.log(arr);
// [1, 2, 10, 20]